
package DAO;

import DTO.CAUTHU;
import java.sql.Connection;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import javax.swing.JOptionPane;




public class CauthuDAO {
  Connect con = new Connect();
  Connection cont;
  // tao ket noi
  public CauthuDAO()
  {
      cont = con.Connect("sa","123456");
      
  }
  public ResultsModel GetData()
  {
      Result rs = new Result();
      String sqlGetData="Select * from View_cauthu";
      String []title= {"Mã CT","Mã ĐB","Tên CT","Ngày Sinh","Quốc Tịch","Vị Trí","Số áo","Chiều cao","Cân nặng"};
      return rs.GetResult(cont,sqlGetData,title);
  }
  public void Insert (CAUTHU ct)
  {
      ArrayList <String>a = new ArrayList();
      a.add("Thongbao");
      a.add(ct.getNguoi().getManguoi());
      a.add(ct.getNguoi().getHoten());
    //  a.add("05/03/1991");
      SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
      String formattedDate = formatter.format(ct.getNguoi().getDate()); 
      //JOptionPane.showMessageDialog(null,formattedDate,"Thong bao",JOptionPane.INFORMATION_MESSAGE);
      a.add(formattedDate);
      a.add(ct.getNguoi().getQuoctich());
      a.add(ct.getDoibong().getMaDB());
      a.add(ct.getVitri());
      a.add(Integer.toString(ct.getSoao()));
      a.add(Integer.toString(ct.getChieucao()));
      a.add(Integer.toString(ct.getCannang()));
      
      String sqlThemCT= "{Call spInsertCauthu(?,?,?,?,?,?,?,?,?,?)}";
      con.Insert(sqlThemCT, a, true);
  }
  public void Update(CAUTHU ct)
  {
      ArrayList <String>a = new ArrayList();
       a.add("Thongbao");
      a.add(ct.getNguoi().getManguoi());
      a.add(ct.getNguoi().getHoten());
    //  a.add("05/03/1991");
      SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
      String formattedDate = formatter.format(ct.getNguoi().getDate()); 
      //JOptionPane.showMessageDialog(null,formattedDate,"Thong bao",JOptionPane.INFORMATION_MESSAGE);
      a.add(formattedDate);
      a.add(ct.getNguoi().getQuoctich());
      a.add(ct.getDoibong().getMaDB());
      a.add(ct.getVitri());
      a.add(Integer.toString(ct.getSoao()));
      a.add(Integer.toString(ct.getChieucao()));
      a.add(Integer.toString(ct.getCannang()));
      
      String sqlThemCT= "{Call spUpdateCauthu(?,?,?,?,?,?,?,?,?)}";
      con.Update(sqlThemCT, a, true);
  }
  public void Delete(CAUTHU ct)
  {
      String a = ct.getNguoi().getManguoi();
      String sqlXoaCT="{Call spXoaCauthu(?)}";
      con.Delete(sqlXoaCT, a, true);
  }
     public String LayMaCuoi()
      {
          String str="";
          try
          {
          String query = "select MA_CT from CAUTHU order by MA_CT desc";
          ResultSet rs = null;
          rs = con.GetdataTable(query);
          rs.next();
          str = rs.getString(1);
          }
          catch(Exception ex)
          {}
          return str;
      }
}
